package com.scalar.forecast.forecastEntry.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.forecastEntry.forms.ForecastEntryForm;
import com.scalar.forecast.forecastEntry.dao.ForecastEntrySearchDAO;
import com.scalar.forecast.forecastEntry.dao.ForecastEntryDAO;
import com.scalar.forecast.forecastEntry.*;
import com.scalar.forecast.forecastSetup.ForecastTimeFrame;
import com.scalar.forecast.forecastSetup.ForecastEntryField;
import com.scalar.forecast.forecastSetup.dao.ForecastSetupDAO;
import com.scalar.forecast.forecastSetup.dao.ForecastUserFieldsDAO;
import com.scalar.forecast.commons.Constants;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.HashMap;

/**
 * User: Sujan Kumar
 * Date: Feb 14, 2005
 * Time: 12:59:55 PM
 */
public class ForecastEntrySalesNewCommand extends ActionCommand
{
    public ActionForward execute ( ActionMapping mapping,
                                            ActionForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response )
    {
        HttpSession session = request.getSession();
        User pUser = ( User )session.getAttribute( SessionKeys.USER_KEY );
        ForecastEntryForm forecastEntryForm = ( ForecastEntryForm )form;
        if ( forecastEntryForm == null )
        {
            forecastEntryForm = new ForecastEntryForm();
        }
        HashMap errors = new HashMap ();
        HashMap info = new HashMap ();
        try
        {
            if ( ! ForecastEntryDAO.isForecastEntryAllowed( forecastEntryForm.getStartMonth_Quarter_Year(), forecastEntryForm.getYear() ) )
            {
                info.put ( INFO_FORECAST_ENTRY_TIME_ELAPSED, null );
                request.setAttribute( SessionKeys.INFO_MESSAGES, info );
                return mapping.findForward ( "searchPage" );
            }
        }
        catch ( ScalarException e )
        {
            errors.put( ERROR_INTERNAL_SERVER_ERROR, " while calculating entry allowed" );//todo..
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            System.out.println ( "----------exception in Getting search fields in sales entry new command ----" );
            e.printStackTrace();
            return mapping.findForward ( "searchPage" );
        }
        ArrayList searchFieldList = new ArrayList();
        try
        {
            searchFieldList = ForecastEntrySearchDAO.getForecastSetupSearchFields(null);
            loadSearchFieldListPropertyValue( forecastEntryForm, searchFieldList );
        }
        catch ( Exception e )
        {
            errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_LOAD_SEARCH_FIELDS );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            System.out.println ( "----------exception in Getting search fields in sales entry new command ----" );
            e.printStackTrace();
            return mapping.findForward ( "searchPage" );
        }
        Collections.sort( searchFieldList, new SearchFieldListComparator() );
        forecastEntryForm.setSearchFieldList( searchFieldList);

        String userId = pUser.getUserId();
        TableMetaData tableMetaData = getTableMetaData( forecastEntryForm, userId );
        loadUserDefinedFieldValuesInRequest ( tableMetaData.getTableColumnList(), request );
        forecastEntryForm.setTableMetaData( tableMetaData );
        LinkedList rowDataList = null;
        LinkedList actualRowDataList = new LinkedList();
        try
        {
            if ( forecastEntryForm.isBuild() )
            {
                request.setAttribute ( Constants.FORECAST_IS_BUILD, "true" );
                ArrayList maxlevelSalesPersonIds = ForecastEntryDAO.getMaxLevelSalesPersonIds ( null );
				//todo what to do if no maxlevelsalesPersons...
				if ( maxlevelSalesPersonIds == null || maxlevelSalesPersonIds.size() == 0 )
				{
					info.put ( "Sales Person(s) not exists at top level", null );
                	request.setAttribute( SessionKeys.INFO_MESSAGES, info );
                	return mapping.findForward ( "searchPage" );
				}
                rowDataList = getRowDataList( maxlevelSalesPersonIds, true );
            }
            else
            {
                String sPersonId = ( String )session.getAttribute( SessionKeys.USER_SALESPERSON_ID );
                int salesPersonId = -1;
                try
                {
                    salesPersonId = Integer.parseInt( sPersonId );
                }
                catch ( NumberFormatException e ){ }
                ArrayList salesPersonIds = new ArrayList();
                salesPersonIds.add ( new Integer ( salesPersonId ) );
                rowDataList = getRowDataList( salesPersonIds, false);
                actualRowDataList = ( LinkedList )getCopy( rowDataList );
                setActualSlotDataList ( forecastEntryForm, actualRowDataList );
            }
            setSlotDataList ( forecastEntryForm, rowDataList );
            forecastEntryForm.setTableDataList( rowDataList );
            forecastEntryForm.setActualTableDataList( actualRowDataList );
			//Added by sujan on 02 June 2005
			forecastEntryForm.setTableDataListDefaultValues ( ( LinkedList )getCopy( rowDataList ) );
        }
        catch ( Exception e )
        {
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_LOAD_TABLE_DATA );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            System.out.println ( "--Exception while getting the tabledatalist---" );
            e.printStackTrace();
            return mapping.findForward ( "searchPage" );
        }
        try
        {
            ForecastTimeFrame timeframe = ForecastSetupDAO.getForecastTimeFrame(null);
            if ( "Month".equalsIgnoreCase( timeframe.getTfName() ) )
            {
                request.setAttribute ( "showNext", "Months" );
            }
            else if ( "Quarter".equalsIgnoreCase( timeframe.getTfName() ) )
            {
                request.setAttribute ( "showNext", "Quarters" );
            }
            else if ( "Year".equalsIgnoreCase( timeframe.getTfName() ) )
            {
                request.setAttribute ( "showNext", "Years" );
            }
        }
        catch ( Exception e )
        {
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_LOAD_FORECAST_TIMEFRAME );
            request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
            System.out.println ( "----------exception while getting the timeframe-----");
            e.printStackTrace();
            return mapping.findForward ( "searchPage" );
        }
        try
        {
            LinkedList backlogInfoList = getBackLogInfoList( null, forecastEntryForm );
            request.setAttribute( "backlogInfoList", backlogInfoList );
        }
        catch ( ScalarException e )
        {
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_LOAD_BACKLOG_INFO );
            request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
            System.out.println ( "----------exception while getting backlog information-----");
            e.printStackTrace();
            return mapping.findForward ( "searchPage" );
        }
        ActionForward actionForward = mapping.findForward ( "forecastEntrySales");
        return actionForward;
    }
    public static void loadSearchFieldListPropertyValue ( ForecastEntryForm forecastEntryForm, ArrayList searchFieldList )
    {
        SearchField searchField = null;
        int listSize = searchFieldList.size();
        for ( int i=0; i < listSize; i++ )
        {
            searchField = ( SearchField )searchFieldList.get ( i );
            if ( "By Customer".equalsIgnoreCase( searchField.getName() ) && searchField.isDisplay() )
            {
                int custId = forecastEntryForm.getCustId();
                String customerName = "";
                try
                {
                    customerName = CustomerDAO.getCustomerName( null, custId );
                }
                catch ( Exception e )
                {
                    System.out.println ( "---Exception while fetching the customer Name----" );
                    e.printStackTrace();
                }
                searchField.setPropertyValue( customerName );
            }
            if ( "By Project".equalsIgnoreCase( searchField.getName() ) && searchField.isDisplay() )
            {
                int projectId = forecastEntryForm.getProjectId();
                String projectName = "";
                try
                {
                    projectName = ForecastEntrySearchDAO.getProjectName(null, projectId );
                }
                catch ( Exception e )
                {
                    System.out.println ( "--Exception while fetching the projectName ---");
                    e.printStackTrace();
                }
                searchField.setPropertyValue( projectName );
            }
            if ( "By Product".equalsIgnoreCase( searchField.getName() ) && searchField.isDisplay() )
            {
                int productId = forecastEntryForm.getProductId();
                String partNumber = "";
                try
                {
                    partNumber = ForecastEntrySearchDAO.getPartNumber(null, productId );
                }
                catch ( Exception e )
                {
                    System.out.println ( "--Exception while getting the Part Number--");
                    e.printStackTrace();
                }
                searchField.setPropertyValue( partNumber );
            }
            if ( "By ODM".equalsIgnoreCase( searchField.getName() ) && searchField.isDisplay() )
            {
                int odmId = forecastEntryForm.getOdmId();
                String odmName = "";
                try
                {
                    odmName =   CustomerDAO.getCustomerName( null, odmId );
                }
                catch ( Exception e )
                {
                    System.out.println ( "--Exception while getting the ODM Name---" );
                    e.printStackTrace();
                }
                searchField.setPropertyValue( odmName );
            }

            //------setting TimeFrame Field Values
            if ( "Month".equalsIgnoreCase( searchField.getName() ) )
            {
                int month = forecastEntryForm.getStartMonth_Quarter_Year();
                String months[] = { "January", "February", "March","April","May","June","July","August","September","October","November","December" };
                String monthName = "";
                try
                {
                    monthName = months[ month ];
                }
                catch ( IndexOutOfBoundsException e)
                {
                    System.out.println ( "--Exception while loading the month Name----");
                    e.printStackTrace();
                }
                searchField.setPropertyValue( monthName );
            }
            if ( "Quarter".equalsIgnoreCase( searchField.getName() ) )
            {
                int quarter = forecastEntryForm.getStartMonth_Quarter_Year();
                String quarters[] = { "Quarter-1", "Quarter-2", "Quarter-3","Quarter-4" };
				try
				{
					quarters = ForecastEntrySearchDAO.getCurrentYearQuarterNames ( null );
				}
				catch ( ScalarException e ) { }
                String quarterName = "";
                try
                {
                    quarterName = quarters[ quarter ];
                }
                catch ( IndexOutOfBoundsException e)
                {
                    System.out.println ( "--Exception while loading the Quarter Name----");
                    e.printStackTrace();
                }
                searchField.setPropertyValue( quarterName );
            }
            if ( "Year".equalsIgnoreCase( searchField.getName() ) )
            {
                int year = forecastEntryForm.getStartMonth_Quarter_Year();
                searchField.setPropertyValue( Integer.toString( year ) );
            }
            if ( "Year".equalsIgnoreCase( searchField.getDisplayName() ) && "".equalsIgnoreCase( searchField.getName() ) )
            {
                int year = forecastEntryForm.getYear();
                searchField.setPropertyValue( Integer.toString( year ) );
            }
            if ( "showNext".equalsIgnoreCase( searchField.getName() ) )
            {
                int showNext = forecastEntryForm.getShowNext();
                searchField.setPropertyValue( Integer.toString ( showNext ) );
            }
        }
    }
    public static TableMetaData getTableMetaData( ForecastEntryForm forecastEntryForm, String userId )
    {
        int showNext = forecastEntryForm.getShowNext();
        int start = forecastEntryForm.getStartMonth_Quarter_Year();
        int year = forecastEntryForm.getYear();
        TableMetaData tableMetaData = new TableMetaData();

        tableMetaData.setNoOfSlots( showNext );

        ArrayList slotNames = new ArrayList();
        LabelValueBean slotName = null;

        ForecastTimeFrame forecastTimeFrame = null;
        try
        {
            forecastTimeFrame = ForecastSetupDAO.getForecastTimeFrame(null);
        }
        catch ( Exception e )
        {
            System.out.println ( "---Exception while getting the timeframe----" );
            e.printStackTrace();
        }
        String timeFrameName = forecastTimeFrame.getTfName();
        if ( "Month".equalsIgnoreCase( timeFrameName ) )
        {
            String months[] = { "January", "February", "March","April","May","June","July","August","September","October","November","December" };
            StringBuffer slotLabel = new StringBuffer();


            int startMonth = start;
            int startYear = year;
            for ( int i=0; i < showNext; i++ )
            {
                int yearCnt = 0;

                yearCnt = ( i + startMonth ) / 12;
                int monthIdx = ( i + startMonth ) % 12;
                slotLabel.delete( 0, slotLabel.length() );
                slotLabel.append ( months[monthIdx] );
                slotLabel.append ( "-" );
                slotLabel.append ( startYear + yearCnt );
                slotName = new LabelValueBean( slotLabel.toString(), slotLabel.toString() );
                slotNames.add ( slotName );
            }
            tableMetaData.setSlotNames( slotNames );
        }
        else if ( "Quarter".equalsIgnoreCase ( timeFrameName ) )
        {
            String quarters[] = { "Quarter-1", "Quarter-2", "Quarter-3","Quarter-4" };
			try
			{
				quarters = ForecastEntrySearchDAO.getCurrentYearQuarterNames ( null );
			}
			catch ( ScalarException e ) { }
            StringBuffer slotLabel = new StringBuffer();

            int startQuarter = start;
            int startYear = year;
            for ( int i=0; i < showNext; i++ )
            {
                int yearCnt = 0;

                yearCnt = ( i + startQuarter ) / 4;
                int quarterIdx = ( i + startQuarter ) % 4;

                slotLabel.delete( 0, slotLabel.length() );
				try
				{
                	slotLabel.append ( quarters[quarterIdx] );
				}
				catch ( IndexOutOfBoundsException e ) { }
                slotLabel.append ( "-" );
                slotLabel.append ( startYear+yearCnt );
                slotName = new LabelValueBean( slotLabel.toString(), slotLabel.toString() );
                slotNames.add ( slotName );

            }
            tableMetaData.setSlotNames( slotNames );
        }
        else if ( "Year".equalsIgnoreCase ( timeFrameName ) )
        {
            StringBuffer slotLabel = new StringBuffer();
            int startYear = start;
            for ( int i=0; i < showNext; i++ )
            {
                slotLabel.delete( 0, slotLabel.length() );
                slotLabel.append ( Integer.toString ( startYear ) );

                slotName = new LabelValueBean( slotLabel.toString(), slotLabel.toString() );
                slotNames.add ( slotName );
                startYear = startYear + 1;
            }
            tableMetaData.setSlotNames( slotNames );
        }

        ForecastEntryField forecastEntryField = null;
        ArrayList forecastEntryFields = new ArrayList();
        ArrayList tableColumnList = new ArrayList();
        TableColumn tableColumn = null;
        try
        {
            forecastEntryFields = ForecastSetupDAO.getForecastEntryFields(null);
        }
        catch ( Exception e )
        {
            System.out.println ( "--Exception while getting forecast entry fields----" );
            e.printStackTrace();
        }
        int entryFieldsSize = forecastEntryFields.size();
        for ( int i=0; i < entryFieldsSize; i++ )
        {
            forecastEntryField = ( ForecastEntryField ) forecastEntryFields.get ( i );
            if ( forecastEntryField.isEntrySelected() )
            {
                tableColumn = new TableColumn();
                tableColumn.setName( forecastEntryField.getEntryName() );
                tableColumn.setDisplayName( forecastEntryField.getEntryName() );
                tableColumn.setDisplay( forecastEntryField.isEntrySelected() );
                tableColumnList.add ( tableColumn );
            }
        }

        int columnListSize = tableColumnList.size();
        TableColumn editColumn = null;
        for ( int i=0; i < columnListSize; i++ )
        {
            tableColumn = ( TableColumn )tableColumnList.get( i );
            try
            {
                if ( forecastEntryForm.isBuild() ) {
                    editColumn = ForecastEntryDAO.getEditColumn (null, "BUILDFORECAST", tableColumn.getName(), userId );
                } else {
                    editColumn = ForecastEntryDAO.getEditColumn (null, "SALESFORECAST", tableColumn.getName(), userId );
                }
            }
            catch ( Exception e )
            {
                System.out.println ( "----exception while getting the edit column----" );
            }
            if ( editColumn != null )
            {
                tableColumn.setDisplayName( editColumn.getDisplayName() );
                tableColumn.setSortOrder( editColumn.getSortOrder() );
                //tableColumn.setDisplay( editColumn.isDisplay() ); //todo need to set in DAO
            }
            if ( isUserField( tableColumn.getName() ) )
            {
                String columnName = null;
                try
                {
                    columnName = ForecastEntryDAO.getUserDefinedEntryNameColumn (null, tableColumn.getName());
                }
                catch ( Exception e )
                {
                    System.out.println ( "---Exception while getting the column--" );
                    e.printStackTrace();
                }
                if ( columnName != null )
                {
                    tableColumn.setName( columnName );
                }
            }
        }

        tableMetaData.setNoOfTableColumns( getNoOfColumnsToDisplay( tableColumnList ) );
        Collections.sort ( tableColumnList, new TableColumnComparator() );
        tableMetaData.setTableColumnList( tableColumnList );

        return tableMetaData;
    }
    private static boolean isUserField ( String colName )
    {
        if ( "Qty".equalsIgnoreCase( colName ) || "Price".equalsIgnoreCase( colName ) || "Confidence Level".equalsIgnoreCase( colName ) )
        {
            return false;
        }
        return true;
    }

    public static LinkedList getRowDataList(ArrayList salesPersonIds, boolean isBuild) throws Exception
    {
        LinkedList rowDataList = new LinkedList();
        LinkedList stack = new LinkedList();
        LinkedList childRowDataList = null;
        RowData rowData = null;
        int maxLevelOrder = 0;
        int salesPersonIdsSize = salesPersonIds.size();
        Integer salesPersonId = null;
        for ( int i=0; i < salesPersonIdsSize; i++ )
        {
            salesPersonId = ( Integer )salesPersonIds.get ( i );
            rowData = ForecastEntryDAO.getSalesPersonAsRowData(null, salesPersonId.intValue() );
            maxLevelOrder = rowData.getMaxLevelOrder();
            if ( maxLevelOrder == 1 && !isBuild) //todo check whether needed or not...
            {
                rowData.setSelected( true );  //setting this to true becoz in salesentry we don't have checkbox in the screen.
            }
            stack.addFirst( rowData );
        }
        while ( stack.size() != 0 )
        {
            rowData = ( RowData )stack.removeFirst();
            rowDataList.addLast( rowData );
            childRowDataList = ForecastEntryDAO.getChildRowDataList(null, rowData.getSalesPersonId() );
            int childListSize = childRowDataList.size();
            if ( childListSize > 0 )
            {
                rowData.setHasChilds( true );
            }
            for ( int i=0; i < childListSize; i++ )
            {
                rowData = ( RowData ) childRowDataList.get ( i );
                rowData.setMaxLevelOrder( maxLevelOrder );
                stack.addFirst( rowData );
            }
        }
        return rowDataList;
    }

    /**
     * Sets the rowDataList in each rowData in rowDataList.
     * @param forecastEntryForm
     * this form is used to get the search condition field values..
     * @param rowDataList
     * this object will be updated with the slotDataList in each rowData.
     */
    public void setSlotDataList ( ForecastEntryForm forecastEntryForm, LinkedList rowDataList )
    {
        int rowDataListSize = rowDataList.size();
        RowData rowData = null;
        LinkedList slotDataList = null;
        SlotData slotData = null;
        for ( int row=0; row < rowDataListSize; row++  )
        {
            rowData = ( RowData ) rowDataList.get ( row );
            slotDataList = new LinkedList();
            int showNext = forecastEntryForm.getShowNext();
            for ( int slot=0; slot < showNext; slot++ )
            {
                slotData = new SlotData();
                try
                {
                    slotData = ForecastEntryDAO.getSlotData (null, slot, forecastEntryForm, rowData.getSalesPersonId() );
                }
                catch ( Exception e )
                {
                    System.out.println ( "--Exception while getting the slotData---" );
                    e.printStackTrace();
                }
                slotDataList.addLast( slotData );
            }
            rowData.setSlotDataList( slotDataList );
        }
    }
    public static int getNoOfColumnsToDisplay ( ArrayList tableColumnList )
    {
        int noOfColumns = 0;
        TableColumn tableColumn = null;
        if ( tableColumnList != null )
        {
            int listSize = tableColumnList.size();
            for ( int i=0; i < listSize; i++ )
            {
                tableColumn = ( TableColumn )tableColumnList.get ( i );
                if ( tableColumn.isDisplay() )
                {
                    noOfColumns++;
                }
            }
        }
        return noOfColumns;
    }
    public static void loadUserDefinedFieldValuesInRequest ( ArrayList tableColumnList, HttpServletRequest request )
    {
        int listSize = tableColumnList.size();
        TableColumn tableColumn = null;
        ArrayList userFieldValues = new ArrayList();
        for ( int i=0; i < listSize; i++ )
        {
            tableColumn = ( TableColumn ) tableColumnList.get ( i );
            if ( Constants.COLUMN_USERLIST1.equalsIgnoreCase ( tableColumn.getName() ) )
            {
                try
                {
                    String entryName = ForecastUserFieldsDAO.getEntryNameForUserField(null, Constants.COLUMN_USERLIST1 );
                    userFieldValues = ForecastUserFieldsDAO.getUserFieldValues(null, Constants.FORECAST_USER_ENTRY_TABLE_NAME, entryName );
                }
                catch ( Exception e )
                {
                    System.out.println ( "--Exception While loading the user field values ---");
                    e.printStackTrace();
                }
                break;
            }
        }
        userFieldValues.add ( 0, new LabelValueBean ( "-select-","" ) );
        request.setAttribute ( Constants.COLUMN_USERLIST1, userFieldValues );
    }

    public LinkedList getCopy( LinkedList rowDataList )
    {
        LinkedList rowDataListCopy = new LinkedList();
        int rowDataListSize = rowDataList.size();
        RowData rowData = null;
        for ( int i=0 ; i < rowDataListSize; i++ )
        {
            rowData = ( RowData )rowDataList.get ( i );
            rowDataListCopy.add( new RowData ( rowData ) );
        }
        return rowDataListCopy;
    }

    /**
     * This method will sets the actual slot data for each rowData in rowDataList.
     * @param forecastEntryForm
     * @param actualRowDataList
     */
    public void setActualSlotDataList ( ForecastEntryForm forecastEntryForm, LinkedList actualRowDataList ) throws ScalarException
    {
        int actualRowDataListSize = actualRowDataList.size();
        RowData rowData = null;
        LinkedList slotDataList = null;
        SlotData slotData = null;
        for ( int row=0; row < actualRowDataListSize; row++  )
        {
            rowData = ( RowData ) actualRowDataList.get ( row );
            if ( forecastEntryForm.isShowAllSalesPersonsActualValues() ) //todo need to check for the showActualValues
            {
                rowData.setShowActualData( true );
            }
            else if ( forecastEntryForm.isShowActualValues() && row == 0 )
            {
                rowData.setShowActualData( true );
            }
            slotDataList = new LinkedList();
            int showNext = forecastEntryForm.getShowNext();
            for ( int slot=0; slot < showNext; slot++ )
            {
                slotData = new SlotData();
                slotData = ForecastEntryDAO.getActualSlotData (null, slot, forecastEntryForm, rowData.getSalesPersonId() );
                slotDataList.addLast( slotData );
            }
            rowData.setSlotDataList( slotDataList );
        }
    }
    private static RowData getSumAsRowData ( RowData shippedRowData, RowData backlogRowData )
    {
        RowData sumRowData = new RowData();
        LinkedList sumSlotDataList = new LinkedList();
        SlotData shippedSlotData = null, backlogSlotData = null;
        SlotData sumSlotData = null;
        LinkedList shippledSlotDataList = shippedRowData.getSlotDataList();
        LinkedList backlogSlotDataList = backlogRowData.getSlotDataList();
        int slotListSize = shippedRowData.getSlotDataList() == null ? 0 : shippedRowData.getSlotDataList().size();
        for ( int i = 0; i < slotListSize; i++ )
        {
            sumSlotData = new SlotData();
            shippedSlotData = ( SlotData )shippledSlotDataList.get ( i );
            backlogSlotData = ( SlotData )backlogSlotDataList.get ( i );
            sumSlotData.setQty( shippedSlotData.getQty()+backlogSlotData.getQty() );
            sumSlotData.setPrice( shippedSlotData.getPrice() + backlogSlotData.getPrice() );
            sumSlotDataList.add ( sumSlotData );
        }
        sumRowData.setSlotDataList( sumSlotDataList );
        return sumRowData;
    }
    public static LinkedList getBackLogInfoList ( DBTransaction dbTransaction, ForecastEntryForm forecastEntryForm ) throws ScalarException
    {
        RowData openBacklogByRequestedDate = ForecastEntryDAO.getSalesBacklog( dbTransaction, forecastEntryForm, Constants.REQUESTED_DATE_CATEGORY, Constants.BACKLOG_TYPE_OPEN );
        openBacklogByRequestedDate.setSalesPersonName( "Open B/L By Req. Date");
        RowData openBacklogByScheduledDate = ForecastEntryDAO.getSalesBacklog( dbTransaction, forecastEntryForm, Constants.SCHEDULED_DATE_CATEGORY, Constants.BACKLOG_TYPE_OPEN );
        openBacklogByScheduledDate.setSalesPersonName ( "Open B/L By Sch. Date" );

        RowData shippedOrders = ForecastEntryDAO.getShippedOrders( dbTransaction, forecastEntryForm );
        shippedOrders.setSalesPersonName( "Shipped Qty" );

        RowData shippedAndOpenBacklogByReqDateSum = getSumAsRowData ( shippedOrders, openBacklogByRequestedDate );
        shippedAndOpenBacklogByReqDateSum.setSalesPersonName ( "Shipped<br>+<br>B/L By Req. Date");

        RowData shippedAndOpenBackLogBySchDateSum = getSumAsRowData ( shippedOrders, openBacklogByScheduledDate );
        shippedAndOpenBackLogBySchDateSum.setSalesPersonName ( "Shipped<br>+<br>B/L By Sch. Date" );

        LinkedList backlogInfoList = new LinkedList();
        backlogInfoList.add ( openBacklogByRequestedDate );
        backlogInfoList.add ( openBacklogByScheduledDate );
        backlogInfoList.add ( shippedOrders );
        backlogInfoList.add ( shippedAndOpenBacklogByReqDateSum );
        backlogInfoList.add ( shippedAndOpenBackLogBySchDateSum );
        return backlogInfoList;
    }
}
